package karaokestrong.servlets;



import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import karaokestrong.excepcion.DAOExcepcion;
import karaokestrong.modelo.Cliente;
import karaokestrong.modelo.Local;
import karaokestrong.negocio.GestionCliente;
import karaokestrong.negocio.GestionLocal;

/**
 * Servlet implementation class ServletClienteRegistro
 */
@WebServlet("/ServletClienteRegistro")
public class ServletClienteRegistro extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletClienteRegistro() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		GestionCliente clienteReg = new GestionCliente();
		
		try{
		
		Cliente cli = new Cliente();
		
		cli.setDni(Integer.parseInt(request.getParameter("dni")));
		cli.setNombres(request.getParameter("nombre"));
		cli.setPaterno(request.getParameter("paterno"));
		cli.setMaterno(request.getParameter("materno"));
		cli.setCorreoCliente(request.getParameter("correo"));
		cli.setClave(request.getParameter("clave"));
		cli.setTelefono(Integer.parseInt(request.getParameter("telefono")));
		
		if(clienteReg.duplicado(cli) ){
			
			//request.getAttribute("Login_expired");
			//System.out.println("<script>alert('El correo o DNI ya existe!!!')</script>");
			
			//String strExpired = (String)session.getAttribute("Login_Expired");
			//response.sendRedirect("formRegistroCliente.jsp");
			 
			 response.sendRedirect("formRegistroCliente.jsp");
			 
			 
			
		}else{
			clienteReg.insertar(cli, cli);
			
			request.getRequestDispatcher("mensaje.jsp").forward(request, response);
			
		}
		
		
		
		} catch (DAOExcepcion e) {
			System.out.println("Fallo la insercion: " + e.getMessage());
			
			response.sendRedirect("errorlogin.jsp");
		}
		 
		
	}
		
}
